package popup

import android.graphics.Color
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.cardview.widget.CardView
import app.AppContext
import com.hontech.pizzav2.R

class FaultWindow {
    companion object {
        val Instance: FaultWindow by lazy { FaultWindow() }
        val WIDTH = AppContext.resources.getDimension(R.dimen.x400).toInt()
        val HEIGHT = AppContext.resources.getDimension(R.dimen.y500).toInt()
    }

    private val window: PopupWindow
    private val textTitle: TextView
    var isShow = false

    init {
        val params = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        val view = FrameLayout(AppContext)
        view.layoutParams = params
        view.setBackgroundColor(Color.TRANSPARENT)

        val cardView = CardView(view.context)
        val params2 = FrameLayout.LayoutParams(WIDTH, HEIGHT)
        params2.gravity = Gravity.CENTER_HORIZONTAL or Gravity.TOP
        params2.topMargin = 200
        cardView.layoutParams = params2
        cardView.setCardBackgroundColor(0xAA888888.toInt())
        cardView.cardElevation = 5f
        cardView.radius = 10f
        view.addView(cardView)

        val button = Button(view.context)
        val params3 = FrameLayout.LayoutParams(110, 110)
        params3.setMargins(10, 10, 10, 10)
        params3.gravity = Gravity.TOP or Gravity.START
        button.layoutParams = params3
        button.setBackgroundColor(Color.TRANSPARENT)
        button.setOnLongClickListener(::onLongClick)
        view.addView(button)

        val image = ImageView(view.context)
        val params4 = FrameLayout.LayoutParams(150, 150)
        params4.topMargin = 20
        image.scaleType = ImageView.ScaleType.FIT_XY
        image.setImageResource(R.drawable.ic_error)
        image.layoutParams = params4
        params4.gravity = Gravity.CENTER_HORIZONTAL or Gravity.TOP
        cardView.addView(image)

        textTitle = TextView(view.context)
        val params5 = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 300)
        params5.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM
        params5.bottomMargin = 20
        textTitle.setTextColor(Color.BLACK)
        textTitle.layoutParams = params5
        textTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f)
        textTitle.gravity = Gravity.CENTER

        cardView.addView(textTitle)

        window = PopupWindow(
            view, ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, true
        )
    }

    fun showWithTitle(title: String, view: View) {
        textTitle.text = title
        if (isShow) {
            return
        }
        window.showAtLocation(view, Gravity.CENTER, 0, 0)
        isShow = true
    }

    fun dismiss() {
        if (isShow) {
            window.dismiss()
            isShow = false
        }
    }

    private fun onLongClick(view: View): Boolean {
        dismiss()
        return true
    }

}